#include "SDLfunctions.h"

SDL_Surface* init_screen(char *title, int width, int height) {
	SDL_Surface *screen = NULL;
	SDL_Init(SDL_INIT_VIDEO);
	screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
	SDL_WM_SetCaption(title, NULL);
	TTF_Init();
	return screen;
}

bool lock_screen(SDL_Surface *screen) {
    if ( SDL_MUSTLOCK(screen) ) {
        if ( SDL_LockSurface(screen) < 0 ) {
            fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
            return false;
        }
    }
	return true;
}

bool unlock_screen(SDL_Surface *screen) {
	if ( SDL_MUSTLOCK(screen) ) {
        SDL_UnlockSurface(screen);
    }
	return true;
}

void clear_screen(SDL_Surface *screen) {
	SDL_FillRect(screen, NULL, 0);
}

void draw_pixel(SDL_Surface *screen, position_t pixel, Uint32 color) {
	*((Uint32 *)screen->pixels + (screen->h-pixel.y-1) * screen->w + pixel.x) = color;
}

void draw_line(SDL_Surface *screen, position_t p1, position_t p2, Uint32 color) {
	int xmin, xmax;
	int ymin, ymax;
	int i,j;
	float a,b,ii,jj;
	
	if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else {xmin=p2.x; xmax=p1.x;}
	if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else {ymin=p2.y; ymax=p1.y;}
	
	if (xmin==xmax) for (j=ymin;j<ymax;j++) draw_pixel(screen,position_t(xmin,j),color);
	if (ymin==ymax) for (i=xmin;i<xmax;i++) draw_pixel(screen,position_t(i,ymin),color);

	if ((xmax-xmin >= ymax-ymin) && (ymax-ymin>0)) {
		a = (float)(p1.y-p2.y) / ((float)(p1.x-p2.x));
		b = p1.y - a*p1.x;
		for (i=xmin;i<=xmax;i++) {
			jj = a*i+b;
			j = (int) jj;
			if (((jj-j) > 0.5) && (j < screen->h-1)) j++;
			draw_pixel(screen,position_t(i,j),color);
		}
	}
	
	if ((ymax-ymin > xmax-xmin) && (xmax-xmin>0)) {
		a = (float)(p1.y-p2.y) / ((float)(p1.x-p2.x));
		b = p1.y - a*p1.x;
		for (j=ymin;j<=ymax;j++) {
			ii = (j-b)/a;
			i = (int) ii;
			if (((ii-i) > 0.5) && (i < screen->w-1)) i++;
			draw_pixel(screen,position_t(i,j),color);
		}
	}
}

void draw_rect(SDL_Surface *screen, position_t p1, position_t p2, Uint32 color) {
	int xmin, xmax;
	int ymin, ymax;
	int i,j;
	 
	if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
	if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
	
	for (i=xmin;i<=xmax;i++) draw_pixel(screen,position_t(i,ymin),color);
	for (i=xmin;i<=xmax;i++) draw_pixel(screen,position_t(i,ymax),color);
	
	for (j=ymin;j<=ymax;j++) draw_pixel(screen,position_t(xmin,j),color);
	for (j=ymin;j<=ymax;j++) draw_pixel(screen,position_t(xmax,j),color);
}

void draw_fill_rect(SDL_Surface *screen, position_t p1, position_t p2, Uint32 color) {
	int xmin, xmax;
	int ymin, ymax;
	int i,j;
	
	if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
	if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
	
	for (i=xmin;i<=xmax;i++) for (j=ymin;j<=ymax;j++) draw_pixel(screen,position_t(i,j),color);
}

void draw_text(SDL_Surface *screen, char *string, int size, position_t p, Uint32 color) {
	static TTF_Font *font = NULL;
	const char fontName[] = "verdana.ttf";
	SDL_Surface *text = NULL;
	SDL_Rect position;
	SDL_Color c;
	int style = 0;

	if (font) style = TTF_GetFontStyle(font);
	if(!font){
		font = TTF_OpenFont(fontName, size);
	}
	if(!font){
		printf("Failed to open the font \"%s\".\n",fontName);
		printf("Please ensure that the font is present in the directory.\n");
		exit(-1);
	}
	SDL_GetRGB(color, screen->format, &(c.r), &(c.g), &(c.b));
	TTF_SetFontStyle(font, style);
	if (font) text = TTF_RenderText_Blended(font, string, c);
	if (text) {
		position.x = (Sint16) p.x;
		position.y = (Sint16) (screen->h - p.y);
		SDL_BlitSurface(text, NULL, screen, &position);
		SDL_FreeSurface(text);
	}
}